package track2.bayes;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import track2.context.Config;

/**
 * Store the feature information for {@link TrainClassifier}
 * 
 * @author yijia, lingjuan
 * 
 */
public class NaiveBayesFeatures implements Serializable {

	private static final long serialVersionUID = -7281890473640915250L;

	public static final double NOVALUE = -1;

	// store the probability of each feature;
	public double click;

	// titleid_tokensid.txt
	public double[] titleClick;
	public double[] titleNotClick;

	// queryid_tokensid.txt
	public double[] queryClick;
	public double[] queryNotClick;

	// descriptionid_tokensid.txt
	public double[] descriptionClick;
	public double[] descriptionNotClick;

	// purchasedkeywordid_tokensid.txt
	public double[] purchaseClick;
	public double[] purchaseNotClick;

	// userid_profile.txt
	public double[] genderClick;// [0] for male;[1] for female;
	public double[] genderNotClick;

	// [0] for (0, 12], [1] for (12, 18], [2] for (18, 24], [3] for (24, 30],
	// [4] for (30, 40], and [5] for greater than 40.
	public double[] ageClick;
	public double[] ageNotClick;

	public NaiveBayesFeatures() {

		titleClick = new double[Config.tokenSize];
		titleNotClick = new double[Config.tokenSize];
		queryClick = new double[Config.tokenSize];
		queryNotClick = new double[Config.tokenSize];
		descriptionClick = new double[Config.tokenSize];
		descriptionNotClick = new double[Config.tokenSize];
		purchaseClick = new double[Config.tokenSize];
		purchaseNotClick = new double[Config.tokenSize];
		genderClick = new double[3];
		genderNotClick = new double[3];
		ageClick = new double[7];
		ageNotClick = new double[7];

		click = 0.0;
		// initialize
		for (int i = 0; i < Config.tokenSize; i++) {
			queryClick[i] = NOVALUE;
			queryNotClick[i] = NOVALUE;
			titleClick[i] = NOVALUE;
			titleNotClick[i] = NOVALUE;
			descriptionClick[i] = NOVALUE;
			descriptionNotClick[i] = NOVALUE;
			purchaseClick[i] = NOVALUE;
			purchaseNotClick[i] = NOVALUE;
		}
		for (int i = 0; i < genderClick.length; i++) {
			genderClick[i] = NOVALUE;
			genderNotClick[i] = NOVALUE;
		}
		for (int i = 0; i < ageClick.length; i++) {
			ageClick[i] = NOVALUE;
			ageNotClick[i] = NOVALUE;
		}

	}

	public static void putObject(NaiveBayesFeatures feature) {
		try {
			File file = new File(Config.naiveBayesFeaturePath);
			FileOutputStream fs = new FileOutputStream(file);
			ObjectOutputStream os = new ObjectOutputStream(fs);
			os.writeObject(feature);
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static NaiveBayesFeatures getObject() {
		NaiveBayesFeatures feature = null;
		try {
			File file = new File(Config.naiveBayesFeaturePath);
			FileInputStream fs = new FileInputStream(file);
			ObjectInputStream os = new ObjectInputStream(fs);
			feature = (NaiveBayesFeatures) os.readObject();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return feature;

	}

}
